use xloop::prelude::*;

#[allow(dead_code)]
fn main() {
	Application::log_info();
	Application::run(AppRep::default()).ok();
}

#[derive(Debug, Default)]
pub struct AppRep {}
impl xloop_types::AppRep<Application> for AppRep {
	fn on_launch(&self, _: <Application as xloop_types::App>::Ref<'_>) {
		Window::win("main", Default::default(), WinRep {}).ok();
	}
}
pub struct WinRep {}
impl xloop_types::WinRep<Window> for WinRep {
	fn on_create(&self, win: WinRef<'_>) {
		win.show();
		log::warn!("====WinDelegate created")
	}

	fn on_start(&self, _: WinRef<'_>) {
		log::warn!("====WinDelegate started")
	}

	fn on_resume(&self, _: WinRef<'_>) {
		log::warn!("====WinDelegate resume")
	}

	fn on_pause(&self, _: WinRef<'_>) {
		log::warn!("====WinDelegate pause")
	}

	fn on_stop(&self, _: WinRef<'_>) {
		log::warn!("====WinDelegate stoped")
	}

	fn on_destroy(&self, _: WinRef<'_>) {
		log::warn!("====WinDelegate destroy")
	}

	fn on_req_close(&self, _: WinRef<'_>) -> bool {
		true
	}

	fn on_proxy(&self, win: WinRef<'_>, id: i64) {
		println!("proxy {}", id);
		win.fresh("on_proxy");
	}

	fn on_resize(&self, _: WinRef<'_>, size: (u32, u32)) {
		log::warn!("====WinDelegate resized {} {}", size.0, size.1)
	}

	fn on_req_draw(&self, win: WinRef<'_>, nanos: i64) {
		let size = win.size();
		log::warn!("====WinDelegate draw :{size:?}: :{nanos}:")
	}

	fn on_mouse(&self, win: WinRef<'_>, evt: EvtMouse<'_>) -> bool {
		if let Some(evt) = evt.to_evt(win) {
			let evt: &xloop_types::Mouse = &evt;
			match evt.phase {
				xloop_types::Phase::Update => {}
				_ => println!("{:?}", evt),
			}
		}
		true
	}
	fn on_wheel(&self, win: WinRef<'_>, evt: EvtWheel<'_>) -> bool {
		if let Some(evt) = evt.to_evt(win) {
			let evt: &xloop_types::Wheel = &evt;
			println!("{:?}", evt);
		}
		true
	}
	fn on_keyboard(&self, win: WinRef<'_>, evt: EvtKey<'_>) -> bool {
		// println!("{:?}", unsafe { evt.0.characters() });
		if let Some(evt) = evt.to_evt(win) {
			let evt: &xloop_types::Keyboard = &evt;
			println!("{:?}", evt);
		}
		true
	}
	fn on_ime(&self, _: WinRef<'_>, evt: EvtIme) -> bool {
		match evt.action {
			xloop_types::Action::Begin => println!("composition begin"),
			xloop_types::Action::Update => println!("composition update {:?}", evt.data),
			xloop_types::Action::End => println!("composition end {:?}", evt.data),
			xloop_types::Action::Back => println!("composition back"),
		}
		true
	}
}
impl Drop for WinRep {
	fn drop(&mut self) {
		log::warn!("=====win rep drap");
	}
}
